//
//  MainViewController.swift
//  weibo10
//
//  Created by cai on 16/4/13.
//  Copyright © 2016年 cai. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
//MARK 点击按钮
   
    @objc private func composedButtonClick(){
        print("==================")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        /**   给MainViewController添加子控制器(添加四个navigationController子控制器,在tabbar上显示四个button)  */
        addChildViewControllers()
        /** 添加加号按钮 */
        addComposedButton()
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubviewToFront(composedButton)
        
    }
    private lazy var composedButton:UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
//    {
//        let button = UIButton()
//        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
//        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
//        button .sizeToFit()
//
//        return button
//    }()
    
}
//MARK  设置界面

extension MainViewController{
    private func addComposedButton(){
        tabBar.addSubview(composedButton)
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count)
        
        composedButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
    }
    private func addChildViewControllers() {
        tabBar.tintColor = UIColor.orangeColor()
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController (UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        composedButton.addTarget(self, action: "composedButtonClick", forControlEvents: .TouchUpInside)
    }
    //添加控制器
    private func addChildViewController(vc:UIViewController,title:String, imageName:String ) {
        let vc = HomeTableViewController()
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
        
    }
    
}